﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace LcfContols
{
    /// <summary>
    /// 开关按钮
    /// </summary>
    public class LBoolButton:Control
    {
        bool isCheck = false;

        public LBoolButton()
        {
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);

            this.SetStyle(ControlStyles.DoubleBuffer, true);

            this.SetStyle(ControlStyles.ResizeRedraw, true);

            this.SetStyle(ControlStyles.Selectable, true);

            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);

            this.SetStyle(ControlStyles.UserPaint, true);

            this.BackColor = Color.Transparent;

            this.Cursor = Cursors.Hand;

            this.Size = new Size(87, 27);
        }

        /// <summary>
        /// 设置关闭按钮样式
        /// </summary>
        public Bitmap CloseBitmap { get; set; }

        /// <summary>
        /// 设置打开按钮样式
        /// </summary>
        public Bitmap OpenBitmap { get; set; }

        /// <summary>
        /// 是否选中
        /// </summary>
        public bool Checked
        {

            set { isCheck = value; this.Invalidate(); }

            get { return isCheck; }

        }

        protected override void OnPaint(PaintEventArgs e)
        {

            if (CloseBitmap == null)
            {
                CloseBitmap = Properties.Resources.close;
            }
            if (OpenBitmap == null)
                OpenBitmap = Properties.Resources.open;

            Graphics g = e.Graphics;
            Rectangle rec = new Rectangle(0, 0, this.Size.Width, this.Size.Height);
            if (isCheck)
            {
                g.DrawImage(OpenBitmap, rec);
            }
            else
            {
                g.DrawImage(CloseBitmap, rec);

            }
        }

        private void myButtonCheck_Click(object sender, EventArgs e)
        {
            isCheck = !isCheck;
            this.Invalidate();
        }

    }
}
